<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17
 * Time: 14:50
 */

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\CheckPost;
use App\Facades\AdminC;
use App\Admin\Extensions\Comment_isHide;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Encore\Admin\Controllers\ModelForm;
use App\Models\Comments;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Httpful\Request;
use Illuminate\Support\Facades\Input;
use PhpParser\Comment;
use Illuminate\Support\Facades\DB;


class CommentController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('评论列表');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

     public function grid()
     {
         return  AdminC::grid(Comments::class,function(Grid $grid) {
             $grid->filter(function($filter){
                 $model = new Comments();
                 $filter->like('content','内容');
                 $filter->equal('status','状态')->select($model->status_arr);
             });
             $grid->id('Id')->sortable();
             $grid->article_id('文章Id');
             $grid->member_id('用户Id');
             $grid->comment_id('回复评论Id');
             $grid->content('内容');
             $grid->status('状态')->display(function($status){
                 return $this->status_arr[$status];
             });
             $grid->zan('点赞数');
             $grid->date('评论时间');
             $grid->actions(function($action){
                 $info = $action->row;
                 if($info->status){
                     $action->prepend(new Comment_isHide($action->getKey(),$info->article_id, 1));
                 }else{
                     $action->prepend(new Comment_isHide($action->getKey(),$info->article_id, 0));
                 }
                 $action->disableEdit();
                 $action->disableDelete();
             });
             $grid->disableExport();
             $grid->disableCreateButton();
             $grid->tools(function($tools){
                 $tools->batch(function($batch){
                     $batch->disableDelete();
                     $batch->add('显示', new CheckPost( 0));
                     $batch->add('隐藏', new CheckPost( 1));
                 });
             });

         });
     }

    /**
     *  操作显示/隐藏
     */
    public function opreat()
    {
        $input = Input::all();
        $model = Comments::find($input['id']);
        $model->status = $input['status']==1 ? 0 : 1;
//        DB::beginTransaction();
        try{
            if($model->status == 0){
                Article::find($input['article_id'])->increment('comments_num');
            }else{
                Article::find($input['article_id'])->decrement('comments_num');
            }
            $res = $model->save();
            return response()->json([
                'code' => 1,
                'msg' => '成功'
            ]);
        }catch (\Exception $e){
//            DB::rollBack();
            return response()->json([
                'code' => 0,
                'msg' => '失败'
            ]);
        }
//        DB::commit();
    }

    //checkPost 批量验证操作
    public function checkPost()
    {
        $input = Input::all();
        if(isset($input['ids']) && isset($input['action'])) {
            $res = (new Comments())->check($input['ids'], $input['action']);
            if($res) {
                return response()->json(['code'=>1, 'msg'=>'成功']);
            }else{
                return response()->json(['code'=>0, 'msg'=>'失败']);
            }
        }else{
            return response()->json(['code'=>0, 'msg'=>'参数错误']);
        }
    }

}